package com.environment_pro.monitor.scheduler.job;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.environment_pro.monitor.model.TbSystemConf;
import com.environment_pro.monitor.service.TbDeviceAlarmService;
import com.environment_pro.monitor.service.TbDeviceLoadService;
import com.environment_pro.monitor.service.TbDeviceMsgService;
import com.environment_pro.monitor.service.TbSystemConfService;
import com.environment_pro.monitor.util.StaticUtil;


/**
 * 数据备份 轮训job
 * @author yangfeng
 *
 */
public class LoadDataBackupJob implements Job {

	private static final Logger logger = Logger.getLogger(LoadDataBackupJob.class);


	public void execute(JobExecutionContext arg0) throws JobExecutionException  {
		logger.info("JobKey:"+arg0.getJobDetail().getKey());
		logger.info(System.currentTimeMillis());
			TbSystemConfService tbSystemConfService = (TbSystemConfService) StaticUtil.getBean("tbSystemConfService");
			//数据备份设置 
			TbSystemConf backupLoad = tbSystemConfService.findByKeyName("app.data.backup.load");
			if("true".equalsIgnoreCase(backupLoad.getKeyValue())){
				TbSystemConf loadDuration = tbSystemConfService.findByKeyName("app.data.backup.load.duration");
				TbDeviceLoadService tbDeviceLoadService = (TbDeviceLoadService) StaticUtil.getBean("tbDeviceLoadService");
				tbDeviceLoadService.backupData(StaticUtil.null2Integer(loadDuration.getKeyValue()));
			}
			
			TbSystemConf backupAlarm = tbSystemConfService.findByKeyName("app.data.backup.alarm");
			if("true".equalsIgnoreCase(backupAlarm.getKeyValue())){
				TbSystemConf alarmDuration = tbSystemConfService.findByKeyName("app.data.backup.alarm.duration");
				TbDeviceAlarmService tbDeviceAlarmService = (TbDeviceAlarmService) StaticUtil.getBean("tbDeviceAlarmService");
				tbDeviceAlarmService.backupData(StaticUtil.null2Integer(alarmDuration.getKeyValue()));
			}
			
			TbSystemConf backupMsg = tbSystemConfService.findByKeyName("app.data.backup.msg");
			if("true".equalsIgnoreCase(backupMsg.getKeyValue())){
				TbSystemConf msgDuration = tbSystemConfService.findByKeyName("app.data.backup.msg.duration");
				TbDeviceMsgService tbDeviceMsgService = (TbDeviceMsgService) StaticUtil.getBean("tbDeviceMsgService");
				tbDeviceMsgService.backupData(StaticUtil.null2Integer(msgDuration.getKeyValue()));
			}
	}
}
